

#import "LLTableViewController.h"
#import "LLNoDataView.h"

@interface LLTableViewController ()
@property (strong, nonatomic) LLNoDataView *nodateView;
@end

@implementation LLTableViewController

-(LLUserInfo *)userInfo{
    if (!_userInfo) {
        _userInfo = [LLUserInfo sharedUser];
    }
    return _userInfo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (!self.tableViewStyle) {
        self.tableViewStyle = UITableViewStylePlain;
    }
    // 允许晚一会再配置tableView
    if (!self.notConfigTableViewRightAway) {
        [self configTableView];
    }
    
}

- (void)configTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight - self.navH) style:self.tableViewStyle];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:_tableView];
    [self configRefresh];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)configRefresh {
    if (self.needHeaderRefresh || self.needRefresh) {
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithHeader)];
        // 隐藏时间
        header.lastUpdatedTimeLabel.hidden = YES;
        // 隐藏状态
        header.stateLabel.textColor = LLColorRGB153;
        self.tableView.mj_header = header;
    }
    if (self.needRefresh) {
        MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithFooter)];

//        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithFooter)];
        footer.stateLabel.textColor = LLColorRGB(200);
        [footer setTitle:@"" forState:MJRefreshStateIdle];
        [footer setTitle:@"正在加载中 ..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"已全部加载" forState:MJRefreshStateNoMoreData];
        self.tableView.mj_footer = footer;
    }
}

- (void)refreshDataWithFooter {
    if (self.pageModels.count) {
        if (self.pageModels.count < self.pageSize) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            self.page ++;
            [self loadNetworkData];
        }
    }
}

- (void)refreshDataWithHeader {
    self.page = 1;
    [self loadNetworkData];
}

- (void)loadNetworkData {
}

- (void)showNoDataView:(NSString *)imageName title:(NSString *)title {
    [self.nodateView setNoDataView:imageName title:title];
    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState |UIViewAnimationOptionCurveEaseInOut animations:^{
        self.nodateView.alpha = 1.0;
        self.nodateView.hidden = NO;
    } completion:nil];
}
- (void)showNoDataViewTitle:(NSString *)title {
    [self.nodateView setNoDataViewTitle:title];
    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState |UIViewAnimationOptionCurveEaseInOut animations:^{
        self.nodateView.alpha = 1.0;
        self.nodateView.hidden = NO;
    } completion:nil];
}

- (void)hideNoDataView {
    [UIView animateWithDuration:0.05 delay:0 options:UIViewAnimationOptionBeginFromCurrentState |UIViewAnimationOptionCurveEaseInOut animations:^{
        self.nodateView.alpha = 0.0;
        self.nodateView.hidden = YES;
    } completion:nil];
}


#pragma mark - UITableViewDataSoure
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return self.firstMarginZero ? 0 : self.cellMargin;
    }
    return self.cellMargin;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.canEditRow;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (LLNoDataView *)nodateView {
    if (_nodateView == nil) {
        _nodateView = [[LLNoDataView alloc] initWithFrame:self.tableView.bounds];
        [self.tableView addSubview:_nodateView];
    }
    return _nodateView;
}

@end
